package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.news.ProblemInformationActivity;
import com.renwei.yunlong.adapter.ProblemRelateAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.ProblemRelateBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;

import butterknife.Bind;

/**
 * Created by wangqi on 2017/11/28.
 */

public class ProblemRelatedFragment extends BaseLazyFragment implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private String questionId;
    private ProblemRelateAdapter adapter;

    public ProblemRelatedFragment(String questionId) {
        this.questionId = questionId;
    }

    public ProblemRelatedFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_related;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ProblemRelateAdapter(getContext());
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)){
            map.put("currentUserId",ownerBean.getRows().getEmployeeId());
        }else{
            map.put("currentUserId",serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("questionId",questionId);
        ServiceRequestManager.getManager().getProblemRelate(getContext(), JsonMapUtil.mapToJson(map),this);
    }


    @Override
    public void onSuccess(int requestId, String result) {
        ProblemRelateBean bean = new Gson().fromJson(result, ProblemRelateBean.class);
        if (bean.getMessage().getCode()==200){
            adapter.setData(bean.getRows());
            if (bean.getRows()==null||bean.getRows().size()==0){
                stateLayout.showEmptyView();
            }else{
                stateLayout.showContentView();
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }

    @Override
    public void onItemClick(View view, int position) {
        ProblemRelateBean.RowsBean bean = adapter.getItem(position);
        ProblemInformationActivity.OpenActivity(getContext(),bean.getRequestId()+"",bean.getQuestionId()+"","0");
    }
}
